<?php
require_once WSR_DIR . 'activationparams.class.php';
require_once WSR_DIR . 'activation.class.php';
require_once WSR_DIR . 'triplecache.class.php';
require_once WSR_DIR . 'sparqlsource.class.php';

class LargeScaleSpreadingActivation {
    var  $cached_triples;
    var  $activation;
    var  $seed_entity_list;
	var  $params;
	var  $nos_internal_cycles = 5;  // number of times do SA loop internally before fetching fresh triples
	var  $use_fanout = true;
	var  $use_meta = false;

    function __construct() {
		$this->default_params();
        $this->clear();
    }
	function default_params() {
		$this->params = new ActivationParams();
		$this->params->load_threshold = 2;
		$this->params->spread_factor = 0.2;
		$this->_sync_params();
	}
	function _sync_params() {
		if ( $this->activation ) $this->activation->set_params($this->params);
	}
	function set_params($params) {
		$this->params = $params;
		$this->_sync_params();
	}
    function clear() {
		$this->source = new SparqlSource();
        $this->cached_triples = new TripleCache($this->source);
        $this->activation = new Activation();
		$this->_sync_params();
    }
    function activate($seed_entity_list) {
        $this->activation->set_initial_activated_entities($seed_entity_list);
        $this->cached_triples->update($seed_entity_list);
    }
    function iterate($nos_iterations=1) {
        for( $i=0; $i<$nos_iterations; $i++ ) {
 			$graph = $this->cached_triples->get_graph();
 			$meta = $this->cached_triples->get_meta();
           	$axgraph = new AxGraph($graph);
			$this->activation->set_graph($axgraph);
			if ( $this->use_meta ) {
				$this->activation->set_meta($meta);
			}

			$this->activation->do_steps($this->nos_internal_cycles);
			$fresh_entities = $this->activation->get_loadable_entities();
			$fresh_entity_fanouts = $this->activation->get_loadable_entities_with_fanout();
			$unseen_entities = $this->cached_triples->unseen($fresh_entities);
			$loadable_entities = $this->cached_triples->loadable($fresh_entity_fanouts);
			echo "\n======== " . $i . " LOADING " . count($unseen_entities) . ":" .  count($loadable_entities) . " [".$this->activation->params->load_threshold.":".$this->activation->params->load_fanout_threshold."] =========\n";
			//print_r($loadable_entities); echo "\n";
			//echo implode("\n",$unseen_entities);

			if ( $this->use_fanout ) {
				$this->cached_triples->update_with_fanout($fresh_entity_fanouts);
			} else {
            	$this->cached_triples->update($fresh_entities);
			}
			//echo $this->cached_triples->graph->to_ntriples(); echo "\n";
			//echo $this->cached_triples->graph->to_turtle(); echo "\n";
		
			$nos_loaded = $this->get_nos_loaded();
			$nos_triples = $this->get_nos_triples();
			echo "\n======== " . $nos_loaded . " : " . $nos_triples . " =========\n";
        }
    }
	function add_graph($new_graph) {
		$this->cached_triples->add_graph($new_graph);
	}
	function get_graph() {
		return $this->cached_triples->get_graph();
	}
	function get_load_threshold() {
		return $this->params->load_threshold;
	}
	function set_load_threshold($load_threshold) {
		$this->params->load_threshold = $load_threshold;
		$this->_sync_params();
	}
	function get_load_fanout_threshold() {
		return $this->params->load_fanout_threshold;
	}
	function set_load_fanout_threshold($load_fanout_threshold) {
		$this->params->load_fanout_threshold = $load_fanout_threshold;
		$this->_sync_params();
	}
	function get_nos_internal_cycles() {
		return $this->nos_internal_cycles;
	}
	function set_nos_internal_cycles($nos_internal_cycles) {
		$this->nos_internal_cycles = $nos_internal_cycles;
	}
	function set_spread_factor($spread_factor) {
		$this->params->spread_factor = $spread_factor;
		$this->_sync_params();
	}
	function get_spread_factor() {
		return $this->params->spread_factor;
	}
	function get_activation() {
		return $this->activation->activation_levels;
	}
	function get_nos_loaded() {
		return count($this->cached_triples->loaded_entities);
	}
	function get_nos_triples() {
		return count($this->cached_triples->graph->get_triples());
	}
}


?>
